<template>
  <el-container class="layout-container">
    <!-- 顶部导航栏 -->
    <el-header class="header">
      <div class="header-left">
        <span class="system-title">民大MBTI管理系统</span>
      </div>
      <div class="header-right">
        <span class="user-name">管理员：{{ userInfo.username }}</span>
        <el-button 
          type="text" 
          @click="handleLogout" 
          class="logout-btn"
          :icon="SwitchButton"
        >
          退出登录
        </el-button>
      </div>
    </el-header>
    
    <el-container>
      <!-- 左侧菜单栏 -->
      <el-aside width="220px" class="aside">
        <el-menu
          :default-active="activeMenu"
          class="menu"
          router
          background-color="#f8fafc"
          text-color="#334155"
          active-text-color="#3b82f6"
          :collapse="isCollapse"
        >
          <el-sub-menu index="1">
            <template #title>
              <el-icon><School /></el-icon>
              <span>学院专业班级管理</span>
            </template>
            <el-menu-item index="/layout/college">
              <template #title>
                <span>学院管理</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/layout/major">
              <template #title>
                <span>专业管理</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/layout/class">
              <template #title>
                <span>班级管理</span>
              </template>
            </el-menu-item>
          </el-sub-menu>
          
          <el-sub-menu index="2">
            <template #title>
              <el-icon><User /></el-icon>
              <span>用户管理</span>
            </template>
            <el-menu-item index="/layout/teacher">
              <template #title>
                <span>教师管理</span>
              </template>
            </el-menu-item>
            <el-menu-item index="/layout/student">
              <template #title>
                <span>学生管理</span>
              </template>
            </el-menu-item>
          </el-sub-menu>
          
          <el-menu-item index="/layout/questions">
            <template #title>
              <el-icon><Collection /></el-icon>
              <span>题库管理</span>
            </template>
          </el-menu-item>
        </el-menu>
      </el-aside>
      
      <!-- 右侧内容区域 -->
      <el-main class="main">
        <div class="main-content">
          <router-view v-slot="{ Component }">
            <transition name="fade" mode="out-in">
              <component :is="Component" />
            </transition>
          </router-view>
        </div>
      </el-main>
    </el-container>
  </el-container>
</template>

<script setup>
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { ElMessageBox, ElMessage } from 'element-plus'

const router = useRouter()
const route = useRoute()
const userInfo = ref(JSON.parse(localStorage.getItem('xm-pro-user')) || {})
const isCollapse = ref(false)

// 计算当前激活的菜单
const activeMenu = computed(() => {
  return route.path
})

// 退出登录
const handleLogout = () => {
  ElMessageBox.confirm('确定要退出登录吗?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
    customClass: 'logout-message-box'
  }).then(() => {
    localStorage.removeItem('xm-pro-user')
    router.push('/login')
    ElMessage.success('退出成功')
  }).catch(() => {
    // 取消退出
  })
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
  overflow: hidden;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  color: white;
  padding: 0 24px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  z-index: 10;
}

.system-title {
  font-size: 20px;
  font-weight: bold;
  letter-spacing: 1px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.user-name {
  font-size: 14px;
  font-weight: 500;
}

.logout-btn {
  color: white;
  padding: 8px 12px;
  border-radius: 4px;
  transition: all 0.3s;
}

.logout-btn:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.aside {
  background-color: #f8fafc;
  transition: width 0.3s;
  height: calc(100vh - 60px);
  border-right: 1px solid #e2e8f0;
}

.menu {
  border-right: none;
  height: 100%;
}

.menu:not(.el-menu--collapse) {
  width: 220px;
}

.menu .el-menu-item,
.menu .el-sub-menu__title {
  height: 48px !important;
  line-height: 48px !important;
  transition: all 0.2s;
}

.menu .el-menu-item:hover,
.menu .el-sub-menu__title:hover {
  background-color: #eef2ff !important;
  color: #3b82f6 !important;
}

.menu .el-menu-item.is-active {
  background-color: #eef2ff !important;
  color: #3b82f6 !important;
  font-weight: 500;
}

.main {
  padding: 20px;
  background-color: #f1f5f9;
  height: calc(100vh - 60px);
  overflow-y: auto;
}

.main-content {
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
  min-height: calc(100% - 40px);
  padding: 24px;
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>

<style>
/* 全局样式调整 */
.logout-message-box {
  min-width: 300px;
}

/* 菜单图标样式 */
.el-menu-item .el-icon,
.el-sub-menu__title .el-icon {
  font-size: 18px;
  margin-right: 8px;
}

/* 子菜单缩进 */
.el-menu--inline {
  background-color: #f8fafc !important;
}

.el-menu--inline .el-menu-item {
  padding-left: 50px !important;
}
</style>